/**
* jQuery ligerUI 1.3.2
*
* http://ligerui.com
*
* Author daomi 2015 [ gd_star@163.com ]
*
*/
(function ($) {
  var l = $.ligerui

  l.windowCount = 0

  $.ligerWindow = function (options) {
    return l.run.call(null, 'ligerWindow', arguments, { isStatic: true })
  }

  $.ligerWindow.show = function (p) {
    return $.ligerWindow(p)
  }

  $.ligerDefaults.Window = {
    showClose: true,
    showMax: true,
    showToggle: true,
    showMin: true,
    title: 'window',
    load: false,
    onLoaded: null,
    onClose: null,
    onRegain: null,
    onMax: null,
    modal: false // 是否模态窗口
  }

  $.ligerMethos.Window = {}

  l.controls.Window = function (options) {
    l.controls.Window.base.constructor.call(this, null, options)
  }
  l.controls.Window.ligerExtend(l.core.Win, {
    __getType: function () {
      return 'Window'
    },
    __idPrev: function () {
      return 'Window'
    },
    _extendMethods: function () {
      return $.ligerMethos.Window
    },
    _render: function () {
      var g = this; var p = this.options
      g.window = $('<div class="l-window"><div class="l-window-header"><div class="l-window-header-buttons"><div class="l-window-toggle"></div><div class="l-window-max"></div><div class="l-window-close"></div><div class="l-clear"></div></div><div class="l-window-header-inner"></div></div><div class="l-window-content"></div></div>')
      g.element = g.window[0]
      g.window.content = $('.l-window-content', g.window)
      g.window.header = $('.l-window-header', g.window)
      g.window.buttons = $('.l-window-header-buttons:first', g.window)
      if (p.url) {
        if (p.load) {
          g.window.content.load(p.url, function () {
            g.trigger('loaded')
          })
          g.window.content.addClass('l-window-content-scroll')
        } else {
          var iframe = $("<iframe frameborder='0' src='" + p.url + "'></iframe>")
          var framename = 'ligeruiwindow' + l.windowCount++
          if (p.name) framename = p.name
          iframe.attr('name', framename).attr('id', framename)
          p.framename = framename
          iframe.appendTo(g.window.content)
          g.iframe = iframe
        }
      } else if (p.content) {
        var content = $('<div>' + p.content + '</div>')
        content.appendTo(g.window.content)
      } else if (p.target) {
        g.window.content.append(p.target)
        p.target.show()
      }

      this.mask()

      g.active()

      $('body').append(g.window)

      g.set({ width: p.width, height: p.height })
      // 位置初始化
      var left = 0
      var top = 0
      if (p.left != null) left = p.left
      else p.left = left = 0.5 * ($(window).width() - g.window.width())
      if (p.top != null) top = p.top
      else p.top = top = 0.5 * ($(window).height() - g.window.height()) + $(window).scrollTop() - 10
      if (left < 0) p.left = left = 0
      if (top < 0) p.top = top = 0

      g.set(p)

      p.framename && $('>iframe', g.window.content).attr('name', p.framename)
      if (!p.showToggle) $('.l-window-toggle', g.window).remove()
      if (!p.showMax) $('.l-window-max', g.window).remove()
      if (!p.showClose) $('.l-window-close', g.window).remove()

      g._saveStatus()

      // 拖动支持
      if ($.fn.ligerDrag) {
        g.draggable = g.window.drag = g.window.ligerDrag({
          handler: '.l-window-header-inner',
          onStartDrag: function () {
            g.active()
          },
          onStopDrag: function () {
            g._saveStatus()
          },
          animate: false
        })
      }
      // 改变大小支持
      if ($.fn.ligerResizable) {
        g.resizeable = g.window.resizable = g.window.ligerResizable({
          onStartResize: function () {
            g.active()
            $('.l-window-max', g.window).removeClass('l-window-regain')
          },
          onStopResize: function (current, e) {
            var top = 0
            var left = 0
            if (!isNaN(parseInt(g.window.css('top')))) { top = parseInt(g.window.css('top')) }
            if (!isNaN(parseInt(g.window.css('left')))) { left = parseInt(g.window.css('left')) }
            if (current.diffTop) { g.window.css({ top: top + current.diffTop }) }
            if (current.diffLeft) { g.window.css({ left: left + current.diffLeft }) }
            if (current.newWidth) { g.window.width(current.newWidth) }
            if (current.newHeight) { g.window.content.height(current.newHeight - 28) }

            g._saveStatus()
            return false
          }
        })
        g.window.append("<div class='l-btn-nw-drop'></div>")
      }
      // 设置事件
      $('.l-window-toggle', g.window).click(function () {
        if ($(this).hasClass('l-window-toggle-close')) {
          g.collapsed = false
          $(this).removeClass('l-window-toggle-close')
        } else {
          g.collapsed = true
          $(this).addClass('l-window-toggle-close')
        }
        g.window.content.slideToggle()
      }).hover(function () {
        if (g.window.drag) { g.window.drag.set('disabled', true) }
      }, function () {
        if (g.window.drag) { g.window.drag.set('disabled', false) }
      })
      $('.l-window-close', g.window).click(function () {
        if (g.trigger('close') == false) return false
        g.window.hide()
        l.win.removeTask(g)
      }).hover(function () {
        if (g.window.drag) { g.window.drag.set('disabled', true) }
      }, function () {
        if (g.window.drag) { g.window.drag.set('disabled', false) }
      })
      $('.l-window-max', g.window).click(function () {
        if ($(this).hasClass('l-window-regain')) {
          if (g.trigger('regain') == false) return false
          g.window.width(g._width).css({ left: g._left, top: g._top })
          g.window.content.height(g._height - 28)
          $(this).removeClass('l-window-regain')
        } else {
          if (g.trigger('max') == false) return false
          g.window.width($(window).width() - 2).css({ left: 0, top: 0 })
          g.window.content.height($(window).height() - 28).show()
          $(this).addClass('l-window-regain')
        }
      })
    },
    _saveStatus: function () {
      var g = this
      g._width = g.window.width()
      g._height = g.window.height()
      var top = 0
      var left = 0
      if (!isNaN(parseInt(g.window.css('top')))) { top = parseInt(g.window.css('top')) }
      if (!isNaN(parseInt(g.window.css('left')))) { left = parseInt(g.window.css('left')) }
      g._top = top
      g._left = left
    },
    min: function () {
      this.window.hide()
      this.minimize = true
      this.actived = false
    },
    _setShowMin: function (value) {
      var g = this; var p = this.options
      if (value) {
        if (!g.winmin) {
          g.winmin = $('<div class="l-window-min"></div>').prependTo(g.window.buttons)
            .click(function () {
              g.min()
            })
          l.win.addTask(g)
        }
      } else if (g.winmin) {
        g.winmin.remove()
        g.winmin = null
      }
    },
    _setLeft: function (value) {
      if (value != null) { this.window.css({ left: value }) }
    },
    _setTop: function (value) {
      if (value != null) { this.window.css({ top: value }) }
    },
    _setWidth: function (value) {
      if (value > 0) { this.window.width(value) }
    },
    _setHeight: function (value) {
      if (value > 28) { this.window.content.height(value - 28) }
    },
    _setTitle: function (value) {
      if (value) { $('.l-window-header-inner', this.window.header).html(value) }
    },
    _setUrl: function (url) {
      var g = this; var p = this.options
      p.url = url
      if (p.load) {
        g.window.content.html('').load(p.url, function () {
          if (g.trigger('loaded') == false) return false
        })
      } else if (g.jiframe) {
        g.jiframe.attr('src', p.url)
      }
    },
    hide: function () {
      var g = this; var p = this.options
      this.unmask()
      this.window.hide()
    },
    show: function () {
      var g = this; var p = this.options
      this.mask()
      this.window.show()
    },
    remove: function () {
      var g = this; var p = this.options
      this.unmask()
      this.window.remove()
    },
    active: function () {
      var g = this; var p = this.options
      if (g.minimize) {
        var width = g._width; var height = g._height; var left = g._left; var top = g._top
        if (g.maximum) {
          width = $(window).width()
          height = $(window).height()
          left = top = 0
          if (l.win.taskbar) {
            height -= l.win.taskbar.outerHeight()
            if (l.win.top) top += l.win.taskbar.outerHeight()
          }
        }
        g.set({ width: width, height: height, left: left, top: top })
      }
      g.actived = true
      g.minimize = false
      l.win.setFront(g)
      g.show()
      l.win.setFront(this)
    },
    setUrl: function (url) {
      return _setUrl(url)
    }
  })
})(jQuery)
